﻿using ConmonDateModel.ResponseBase;
using DateDBControl.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MyLogProject;
using MyProjectControlApplication.Interfaces;
using ServiceControl;
using ServiceControl.Helps;
using System.Linq.Expressions;
using System.Net;

namespace MyProjectControlApplication
{
    /// <summary>
    /// 
    /// </summary>
    public class TokenValidationAttribute : ActionFilterAttribute
    {
        private ICacheService _cacheService;
  

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            ResponseBody _result = new ResponseBody();
            var token = context.HttpContext.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
            _cacheService = context.HttpContext.RequestServices.GetRequiredService<ICacheService>();
            LogManager.WriteLog(LogTypeEnum.Normal, "OnActionExecuting", $"收到toekn:{token}");
            var cachedData = _cacheService.GetData<object>(token);
            if (cachedData == null)
            {
                _result.Code = ErrorCode.Unknown;
                _result.Description = "未登录";
                context.Result = new ObjectResult(_result);
                return;
            }

            var _Result = ApiActionAuth.OnAuthorization(token);
            if (_Result.Code == ErrorCode.OK)
            {
                if (_Result.Data != null)
                {
                    UserModel itemUser = _Result.Data as UserModel;
                    _result = AppDataServiceControl.ToGetTargetUser(itemUser.username, itemUser.password);
                    if (_result.Code == ErrorCode.OK)
                    {
                        string _RequestMethod= context.HttpContext.Request.Path;
                        string[] _array = _RequestMethod.Split('/');
                        _RequestMethod= _array.LastOrDefault();
                        if (!string.IsNullOrEmpty(_RequestMethod))
                        {
                            _result =AppDataServiceControl.ToCheckLimitByRole(_RequestMethod, itemUser.role);
                            if (_result.Code == ErrorCode.OK)
                            {
                                base.OnActionExecuting(context); // 继续执行后续操作
                            }
                            else
                            {
                                context.Result = new ObjectResult(_result);
                                
                            }
                        }
                       
                    }
                    else
                    {
                        _result.Description = "校验用户失败";
                        context.Result = new ObjectResult(_result);
                        return;
                    }
                }
                else
                {
                    _result.Description = "Token信息异常";
                    context.Result = new ObjectResult(_result);
                    return;
                }
                
            }
            else 
            {
                _result.Description = "Token校验异常";
                context.Result = new ObjectResult(_result);
                return;
            }
         
           
        }


    }
       
}
